package com.info.zhiduoduo.admin.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.info.zhiduoduo.admin.model.formbean.evaluate.QueryEvaluateTagByConditionFormBean;
import com.info.zhiduoduo.admin.model.formbean.evaluate.SaveEvaluateTagFormBean;
import com.info.zhiduoduo.admin.model.formbean.evaluate.UpdateEvaluateTagFormBean;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.constants.StatusMsg;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.common.utils.StringUtil;
import com.info.zhiduoduo.core.service.common.BaseService;
import com.info.zhiduoduo.repository.entity.evaluate.EvaluateTagConfig;
import com.info.zhiduoduo.repository.mapper.evaluate.EvaluateTagConfigMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Transactional
@Service
public class EvaluateTagService extends BaseService {

	@Resource
	EvaluateTagConfigMapper evaluateTagConfigMapper;

	/**
	 * 新增评价标签
	 * @param formBean
	 * @return
	 */
	public SmartResult saveEvaluateTag(BaseSimpleFormBean<SaveEvaluateTagFormBean> formBean){
		if (null == formBean || null == formBean.getReqdata()
				|| 0 == formBean.getReqdata().getEvaluateType() || StringUtil.isEmpty(formBean.getReqdata().getTagContent())) {
			return errorParamSmartResult();
		}
		SaveEvaluateTagFormBean evaluateTagFormBean = formBean.getReqdata();
		EvaluateTagConfig evaluateTagConfig = new EvaluateTagConfig()
				.setEvaluateType(evaluateTagFormBean.getEvaluateType())
				.setTagContent(evaluateTagFormBean.getTagContent())
				//添加星级
				.setStarLevel(evaluateTagFormBean.getStarLevel());
		if(!evaluateTagConfig.insert()){
			return smartResult(StatusMsg.StatusMsg_199);
		}
		return smartResult();
	}

	/**
	 * 修改评价标签
	 * @param formBean
	 * @return
	 */
	public SmartResult updateEvaluateTag(BaseSimpleFormBean<UpdateEvaluateTagFormBean> formBean){
		if (null == formBean || null == formBean.getReqdata() || 0 == formBean.getReqdata().getId()
				|| 0 == formBean.getReqdata().getEvaluateType() || StringUtil.isEmpty(formBean.getReqdata().getTagContent())) {
			return errorParamSmartResult();
		}
		EvaluateTagConfig evaluateTagConfig = new EvaluateTagConfig().selectById(formBean.getReqdata().getId());
		if(null == evaluateTagConfig){
			return smartResult(StatusMsg.StatusMsg_802);
		}
		evaluateTagConfig.setEvaluateType(formBean.getReqdata().getEvaluateType())
				.setTagContent(formBean.getReqdata().getTagContent())
				.setUpdatetime(new Date())
				//更改星级
				.setStarLevel(formBean.getReqdata().getStarLevel());
		if(!evaluateTagConfig.updateById()){
			return smartResult(StatusMsg.StatusMsg_199);
		}
		return smartResult();
	}

	/**
	 * 作废评价标签
	 * @param formBean
	 * @return
	 */
	public SmartResult deleteEvaluateTag(BaseSimpleFormBean<UpdateEvaluateTagFormBean> formBean){
		if (null == formBean || null == formBean.getReqdata() || 0 == formBean.getReqdata().getId()) {
			return errorParamSmartResult();
		}
		EvaluateTagConfig evaluateTagConfig = new EvaluateTagConfig().selectById(formBean.getReqdata().getId());
		if(null == evaluateTagConfig){
			return smartResult(StatusMsg.StatusMsg_802);
		}
		if(!evaluateTagConfig.setTagState(2).updateById()){
			return smartResult(StatusMsg.StatusMsg_199);
		}
		return smartResult();
	}

	/**
	 * 查询评价标签列表
	 * @param formBean
	 * @return
	 */
	public SmartResult queryEvaluateTagByCondition(BaseSimpleFormBean<QueryEvaluateTagByConditionFormBean> formBean){
		if(null == formBean || null == formBean.getReqdata()){
			return errorParamSmartResult();
		}
		int evaluateType = formBean.getReqdata().getEvaluateType();
		Page<Object> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
		List<EvaluateTagConfig> tagList = evaluateTagConfigMapper.queryEvaluateTagByEvaluateType(evaluateType);
		return smartResult(tagList, page.getTotal(), page.getPages(), page.getPageNum());
	}
}
